package cn.chenyumeng.blog.interceptor;

import cn.chenyumeng.blog.service.admin.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 文章点击拦截器
 *
 * @program: blog->ArticleClickInterceptor
 * @description: 文章点击拦截器
 * @author: cg
 * @create: 2020-10-29 19:24
 **/
@Component
public class ArticleClickInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private BlogService blogService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getServletPath().split("/")[3];
        String id = request.getParameter("id");
        if ("getBlogById".equals(uri) && id != null) {
            blogService.increaseViews(Long.parseLong(id));
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
